package com.zdsoft.blp.gongyong.imageFrame;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.application.MyApp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/24.
 */
public class IvFrameView extends FrameLayout{

    private ViewPager vp;
    private LinearLayout ll_dian;
    private ArrayList<View> pageview;
    private List<ImageView> ivs;
    private Context context;
    public IvFrameView(Context context) {
        super(context);
        init(context);
    }

    public IvFrameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IvFrameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        LayoutInflater.from(context).inflate(R.layout.ivframelayout,this,true);
        this.context = context;
    }

    public void show(String[] images) {
        vp = (ViewPager) findViewById(R.id.vp);
        ll_dian = (LinearLayout) findViewById(R.id.ll_dian);
        pageview = new ArrayList<>();
        ivs = new ArrayList<>();
        LayoutInflater inflater =((Activity)context).getLayoutInflater();
        for(int i =0;i<images.length; i++){
            View view = inflater.inflate(R.layout.firstitem, null);
            ImageView iv= (ImageView) view.findViewById(R.id.iv);
            MyApp.setImage(images[i],iv);
            pageview.add(view);

            ImageView ivdian =new ImageView(context);
            ivdian.setImageResource(R.drawable.icon_point);
            ll_dian.addView(ivdian);
            ivs.add(ivdian);
        }
        setAdapter();
    }
    public void show(List<String> images) {
        vp = (ViewPager) findViewById(R.id.vp);
        ll_dian = (LinearLayout) findViewById(R.id.ll_dian);
        pageview = new ArrayList<>();
        ivs = new ArrayList<>();
        LayoutInflater inflater =((Activity)context).getLayoutInflater();
        for(int i =0;i<images.size(); i++){
            View view = inflater.inflate(R.layout.firstitem, null);
            ImageView iv= (ImageView) view.findViewById(R.id.iv);
            MyApp.setImage(images.get(i),iv);
            pageview.add(view);

            ImageView ivdian =new ImageView(context);
            ivdian.setImageResource(R.drawable.icon_point);
            ll_dian.addView(ivdian);
            ivs.add(ivdian);
        }
        setAdapter();
    }

    private void setAdapter() {
        vp.setAdapter(pagerAdapter);
        //设置第一个页面的选中状态
        ivs.get(0).setImageResource(R.drawable.icon_point_pre);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                clean();
                ivs.get(position).setImageResource(R.drawable.icon_point_pre);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void clean() {
        for(int i =0;i<ivs.size();i++){
            ivs.get(i).setImageResource(R.drawable.icon_point);
        }
    }

    //数据适配器
    PagerAdapter pagerAdapter = new PagerAdapter(){
        @Override
        //获取当前窗体界面数
        public int getCount() {
            return pageview.size();
        }
        @Override
        //断是否由对象生成界面
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0==arg1;
        }
        //是从ViewGroup中移出当前View
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(pageview.get(arg1));
        }
        //返回一个对象，这个对象表明了PagerAdapter适配器选择哪个对象放在当前的ViewPager中
        public Object instantiateItem(View arg0, int arg1){
            ((ViewPager)arg0).addView(pageview.get(arg1));
            return pageview.get(arg1);
        }
    };



}
